﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface.IEarTagReposiotry;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl.EarTagReposiory
{
    public class EarTagRepository : IEarTagReposiotry
    {
        private readonly AgricultureDbContext _context;
        public EarTagRepository(AgricultureDbContext _context)
        {
            this._context = _context;
        }

      
        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="entity">添加实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Add(List<Eartag> entity)
        {

            try
            {
                await _context.AddRangeAsync(entity);

                return await _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<int> Delete(int id)
        {
            try
            {
                var list = _context.Eartags.FirstOrDefault(x => x.Id == id);
                if (list != null)
                {
                    list.Isdel = true;
                    _context.Update(list);
                }
                return _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑批量删除指定耳标
        /// </summary>
        /// <param name="id">要删除的标签的唯一标识符</param>
        /// <returns>一个表示异步操作的任务，任务的结果是受影响的数据库行数</returns>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            foreach (var item in ids)
            {
                var list = _context.Eartags.FirstOrDefault(x => x.Id == item);
                if (list != null)
                {
                    list.Isdel = true;
                    _context.Update(list);
                }
            }
            return await _context.SaveChangesAsync();
        }
      
		/// <summary>
		/// 批量修改耳号
		/// </summary>
		/// <param name="eartag"></param>
		/// <returns></returns>
		public Task<int> UpdateListAsync(List<Eartag> eartag)
        {
            _context.Eartags.UpdateRange(eartag);
            return _context.SaveChangesAsync();
        }
		/// <summary>
		/// 显示未使用的耳标
		/// </summary>
		/// <returns></returns>
		public async Task<IQueryable<Eartag>> ShowStateEartag()
        {

            try
            {
                return _context.Eartags.Where(c => c.EarTagUseStatus == 0).AsQueryable();
            }

            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示功能
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Eartag>> GetShow()
        {
            try
            {
                return _context.Eartags.Where(x => x.Isdel == false);
               
            }
            catch (Exception)
            {

                throw;
            }
        }
   /// <summary>
   /// 反填功能
   /// </summary>
   /// <param name="id">id</param>
   /// <returns>通过ID查找到的列表</returns>
        public async Task<Eartag> Find(int id)
        {
            try
            {
                var list = _context.Eartags.FirstOrDefault(x => x.Id == id);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
                    
        }
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="Death">修改实体</param>
        /// <returns>返回受影响行数</returns>

        public async Task<int> UpdateAsync(Eartag eartag)
        {
            _context.Entry(eartag).State = EntityState.Modified;
            
            return await _context.SaveChangesAsync();
        }
    }
}
